let session = {
  get: function (key) {
    return JSON.parse(sessionStorage.getItem(key))
  },
  set: function (key, value) {
    sessionStorage.setItem(key, JSON.stringify(value))
  },
  remove: function (key) {
    sessionStorage.removeItem(key)
  },
  clear: function () {
    sessionStorage.clear()
  }
}

let local = {
  get: function (key) {
    return JSON.parse(localStorage.getItem(key))
  },
  set: function (key, value) {
    localStorage.setItem(key, JSON.stringify(value))
  },
  remove: function (key) {
    localStorage.removeItem(key)
  },
  clear: function () {
    localStorage.clear()
  }
}

function getUserSettings (keyBaseOn) {
  return JSON.parse(localStorage.getItem(keyBaseOn)) || {}
}

let localBaseOnKey = {
  get: function (key, keyBaseOn) {
    return getUserSettings(keyBaseOn)[key]
  },
  set: function (key, value, keyBaseOn) {
    let curSettings = getUserSettings(keyBaseOn)
    curSettings[key] = value
    localStorage.setItem(keyBaseOn, curSettings)
  },
  remove: function (key, keyBaseOn) {
    let curSettings = getUserSettings(keyBaseOn)
    delete curSettings[key]
    localStorage.setItem(keyBaseOn, curSettings)
  },
  clear: function (keyBaseOn) {
    localStorage.removeItem(keyBaseOn)
  }
}

export default {
  // 基于SessionStorage
  session,
  // 基于LocalStorage
  local,
  // 基于用户Id的LocalStorage
  localBaseOnKey
}
